package com.lanou.zakerproject.disport;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.BaseActivity;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou.zakerproject.tool.GsonRequest;

/**
 * Created by dllo on 16/1/20.
 */
public class DisportGroupActivity extends BaseActivity {
    private ListView listView;
    private TextView textView;
    private DisportGroupAdapter disportGroupAdapter;
    private DisportGroupData disportGroupData;
    private String url;
    private String pk, title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disport_group);
        listView = (ListView) findViewById(R.id.disport_group_listview);
        textView = (TextView) findViewById(R.id.disport_group_title);
        final Intent intent = getIntent();
        pk = intent.getStringExtra("pk");
        title = intent.getStringExtra("title");
        url = "http://wl.myzaker.com/?c=columns&m=more&column_id=" + pk;
        Log.d("DisportGroupActivity", url);
        getRequsetData(url);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent1 = new Intent();
                intent1.setClass(DisportGroupActivity.this, DisportDetailActivity.class);
                String disport_detail_title = disportGroupData.getData().getItems().get(position).getTitle();
                String disport_detail_url = disportGroupData.getData().getItems().get(position).getArticle().getWeburl();
                Log.d("DisportGroupActivity", disport_detail_url);
                intent1.putExtra("disport_detail_url", disport_detail_url);
                intent1.putExtra("disport_detail_title", disport_detail_title);
                startActivity(intent1);
            }
        });
    }

    /**
     * 解析数据的方法
     *
     * @param urls 接口
     */
    private void getRequsetData(String urls) {
        GsonRequest<DisportGroupData> gsonRequest = new GsonRequest<>(urls, DisportGroupData.class, new Response.Listener<DisportGroupData>() {
            @Override
            public void onResponse(DisportGroupData response) {
                disportGroupAdapter = new DisportGroupAdapter(DisportGroupActivity.this, response);
                disportGroupData = response;
                listView.setAdapter(disportGroupAdapter);
                textView.setText(title);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

}


